package com.huawei123.hasee.mynavigationview_view.Palette;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import android.widget.TextView;

import com.huawei123.hasee.mynavigationview_view.R;


/**
 * 使用Palette调色板抓取图片中基色
 * 在build.gradle中加入   compile 'com.android.support:palette-v7:23.1.0'  //palette 颜色比对获取基础色调
 */
public class PaletteMainActivity extends AppCompatActivity {

    private TextView mTV;
    private TextView mTV1;
    private TextView mTV2;
    private TextView mTV3;
    private TextView mTV4;
    private TextView mTV5;
    private TextView mTV6;
    private TextView mTV7;
    private TextView mTV8;
    private ImageView mIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_palette);
        mIV = (ImageView) this.findViewById(R.id.mIV);
        mTV = (TextView) this.findViewById(R.id.mTV);
        mTV1 = (TextView) this.findViewById(R.id.mTV1);
        mTV2 = (TextView) this.findViewById(R.id.mTV2);
        mTV3 = (TextView) this.findViewById(R.id.mTV3);
        mTV4 = (TextView) this.findViewById(R.id.mTV4);
        mTV5 = (TextView) this.findViewById(R.id.mTV5);
        mTV6 = (TextView) this.findViewById(R.id.mTV6);
        mTV7 = (TextView) this.findViewById(R.id.mTV7);
        mTV8 = (TextView) this.findViewById(R.id.mTV8);
        BitmapDrawable drawable = (BitmapDrawable) mIV.getDrawable();//拿到图片的绘制对象
        Bitmap bitmap = drawable.getBitmap();

        //        Palette palette = Palette.generate(bitmap);//得到调色板对象， Palette.generate(bitmap)效率低容易卡死线程不推荐使用，使用下面的监听
        //        int darkMutedColor = palette.getMutedColor(Color.BLUE);//没有颜色则使用默认Color.BLUE
        //        mTV.setBackgroundColor(darkMutedColor);

        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch vs1 = palette.getVibrantSwatch();//充满活力的色板
                Palette.Swatch vs2 = palette.getDarkVibrantSwatch(); //充满活力的暗色类型色板
                Palette.Swatch vs3 = palette.getLightVibrantSwatch(); //充满活力的亮色类型色板
                Palette.Swatch vs4 = palette.getMutedSwatch(); //黯淡的色板
                Palette.Swatch vs5 = palette.getDarkMutedSwatch(); //黯淡的暗色类型色板
                Palette.Swatch vs6 = palette.getLightMutedSwatch(); //黯淡的亮色类型色板
                //vs1.getRgb();谷歌推荐，图片中整体颜色的RGB混合值-----主色调
                if (vs1 != null) {
                    int VibrantSwatch = vs1.getRgb();
                    mTV.setText("VibrantSwatch");
                    mTV.setBackgroundColor(VibrantSwatch);
                }
                if (vs2 != null) {
                    int DarkVibrantSwatch = vs2.getRgb();
                    mTV1.setText("DarkVibrantSwatch");
                    mTV1.setBackgroundColor(DarkVibrantSwatch);
                }
                if (vs3 != null) {
                    int LightVibrantSwatch = vs3.getRgb();
                    mTV2.setText("LightVibrantSwatch");
                    mTV2.setBackgroundColor(LightVibrantSwatch);
                }
                if (vs4 != null) {
                    int MutedSwatch = vs4.getRgb();
                    mTV3.setText("MutedSwatch");
                    mTV3.setBackgroundColor(MutedSwatch);
                }
                if (vs5 != null) {
                    int DarkMutedSwatch = vs5.getRgb();
                    mTV4.setText("DarkMutedSwatch");
                    mTV4.setBackgroundColor(DarkMutedSwatch);
                }
                if (vs6 != null) {
                    int LightMutedSwatch = vs6.getRgb();
                    mTV5.setText("LightMutedSwatch");
                    mTV5.setBackgroundColor(LightMutedSwatch);
                }
                //谷歌推荐的最为标题的颜色值
                int titleTextColor = vs4.getTitleTextColor();
                mTV6.setText("titleTextColor");
                mTV6.setBackgroundColor(titleTextColor);
                //谷歌推荐图片中间文字颜色
                int bodyTextColor = vs4.getBodyTextColor();
                mTV7.setText("bodyTextColor");
                mTV7.setBackgroundColor(bodyTextColor);
                //颜色向量
                float[] hsl = vs4.getHsl();
                //分析图片中所占像素值
                int population = vs4.getPopulation();

                //改变rgb透明度值
                mTV8.setText("透明度rgb颜色");
                mTV8.setBackgroundColor(getTranslucentColor(0.3f, vs5.getRgb()));
            }
        });
    }

    /**
     * 透明度rgb颜色
     *
     * @param percent 透明度
     * @param rgb     rgb颜色
     * @return
     */
    public int getTranslucentColor(float percent, int rgb) {
        //根据rgb值获得三原色与透明度值
        int blue = rgb & 0xff;        //int blue = Color.blue(rgb);
        int green = rgb >> 8 & 0xff;  //int green = Color.green(rgb);
        int red = rgb >> 16 & 0xff;   //int red = Color.red(rgb);
        //透明度
        int alpha = rgb >>> 24;       //int alpha = Color.alpha(rgb);

        alpha = Math.round(alpha * percent);

        return Color.argb(alpha, red, green, blue);
    }

}
